<?php
/**
 * Created by PhpStorm.
 * User: zhangxilu
 * Date: 2017/12/19
 * Time: 下午11:34
 */

namespace common\logic;


use common\models\AuthRoleModel;
use yii\db\ActiveQuery;

class RoleLogic extends Logic
{
    public static $statusList = [
        1 => '开启',
        0 => '关闭',
    ];


    /**
     * @return array|AuthRoleModel[]
     */
    protected function getAll(){
        return $this->_all(['status'=>1],[],[],['id'=>SORT_ASC]);
    }

    /**
     * @param $role_id
     * @return array|AuthRoleModel|null
     */
    public function getById($role_id)
    {
        return $this->_get(['id' => $role_id]);
    }


    public static function getDate()
    {
        return date('Y-m-d H:i:s');
    }

    /**
     * 转换状态
     * @param $status
     * @return mixed
     */
    public static function status_to_str($status)
    {
        return self::$statusList[$status];
    }

    /**
     * 删除角色
     * @param $id
     * @return bool
     */
    public static function deleteRole($id)
    {
        $model = self::findOne($id);
        if ($model) {
            $model->status = 0;
            $model->save();
            return true;
        } else {
            return false;
        }
    }
    
    public function delete($id)
    {
        $model = AuthRoleModel::findOne($id);
        if ($model) {
            $model->status = 0;
            $model->save();
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取所有角色
     * @return array|\yii\db\ActiveRecord[]
     */
    public  function getAllRule()
    {
//  	$query = AuthRoleModel::find();
//		$query->where(['status'=>1]);
//      $query->orderBy(['id'=>SORT_DESC]);
        
        return $this->getAll();
    }

    /**
     * @return ActiveQuery
     */
    protected function _getActiveQuery()
    {
        return AuthRoleModel::find();
    }
    
    public function AuthRoleSave(AuthRoleModel $model)
    {
        $rs = $model->save();
        return $rs;
    }
}